import store from '../store'

export const menusTreeToList = (menus, parent) => {
  let menusList = []
  for (let i = 0; i < menus.length; i++) {
    let menu = menus[i]
    menu = Object.assign({}, menu)
    let children = []
    if (menu.children && menu.children.length > 0) {
      children = menusTreeToList(menu.children, menu)
    }
    delete menu.children

    menu.parent = parent
    menusList.push(menu)
    menusList = menusList.concat(children)
  }
  return menusList
}

export const menusListToMap = (menusList, key) => {
  const menusMap = {}
  if (!key) key = 'code'
  for (let i = 0; i < menusList.length; i++) {
    const menu = menusList[i]
    menusMap[menu[key]] = menu
  }
  return menusMap
}

export const splitMenuLevel = (meta) => {
  let level = []
  let parentLevel = []
  const data = Object.assign({}, meta)
  if (meta.parent && meta.parent.code) {
    parentLevel = splitMenuLevel(meta.parent)
  }
  level = level.concat(parentLevel)
  level.push(data)
  return level
}

export const findMenu = (menus, code) => {
  for (let i = 0; i < menus.length; i++) {
    if (menus[i].code === code) return menus[i]
  }
  return null
}

export const recursionFindMenuByCode = (menus, code) => {
  for (let i = 0; i < menus.length; i++) {
    if (menus[i].code === code) {
      store.state.menus.codeMenu = menus[i]
    } else if (Object.prototype.hasOwnProperty.call(menus[i], 'children') && menus[i].children.length !== 0) {
      recursionFindMenuByCode(menus[i].children, code)
    }
  }
}

// export const recursionFindMenu = (menus, code) => {
//   for (let i = 0; i < menus.length; i++) {
//     if (menus[i].code === code) return menus[i];
//   }
//   return null;
// };

export const findFirstChild = (menu) => {
  if (!menu.children || menu.children.length === 0) return menu
  if (!menu.children[0].children || menu.children[0].children.length === 0) {
    return menu.children[0]
  } else {
    return findFirstChild(menu.children[0])
  }
}

/**
 * @desc 获取需要展示的菜单第一个
 */
export const findMenuFirstChild = (menu) => {
  for (let i = 0; i < menu.length; i++) {
    if (!menu[i].children || menu[i].children.length === 0) {
      return menu[i]
    } else {
      return findMenuFirstChild(menu[i].children)
    }
  }
}

export const recursiveTraverse = function(node, action) {
  if (!node || !node.children) {
    return
  }
  action(node)
  node.children.forEach(function(item, index) {
    recursiveTraverse(item, action)
  })
}

export const findShowMenu = (menu) => {
  if (!menu || menu.length === 0) return
  for (var i = 0; i < menu.length; i++) {
    if (menu[i].hidden) {
      menu.splice(i, 1)
      i--
    } else {
      findShowMenu(menu[i].children)
    }
  }
}

// 找父节点
export const findParent = (nodeList, nodeId) => {
  const ans = []
  for (let i = 0; i < nodeList.length; i++) {
    if (nodeList[i].id === nodeId) {
      if (nodeList[i].parentId === -1) {
        // 根节点
        return [nodeList[i]]
      } else {
        // 非根节点
        ans.push(nodeList[i])
        return findParent(nodeList, nodeList[i].parentId).concat(ans)
      }
    }
  }
  return ans
}

// 把菜单转换成选项当前选项
export const transToOptions = (menus, disableId) => {
  if (!menus) return []
  const options = []
  for (let i = 0; i < menus.length; i++) {
    const node = menus[i]
    let children
    if (node.children && node.children.length > 0) {
      children = transToOptions(node.children, disableId)
    }
    let disabled = false
    if (node.type === 1) {
      // 纯路由
      disabled = true
    }
    if (disableId && node.id === disableId) {
      disabled = true
    }

    options.push({
      label: node.label,
      value: node.id,
      id: node.id,
      parentId: node.parentId,
      disabled: disabled,
      children: children
    })
  }
  return options
}

